(function(){
	var Trajectory = function(id , data , options){
		if(window == this)
			return new Trajectory(id , data , options);	
		this.index = 0;
		this.canvas = document.getElementById(id);
		this.ctx = this.canvas.getContext("2d");
		this.task = null;
		this.temp = [];
		this.list = data;
		this.circle_radius = 5;
		this.money = new Image();
		this.money.src = "images/money.png";
		this.options = options;
		_.bindAll(this,'play','stop','clear','goto','drawTask');
		return this.initialize();
	};
	
	Trajectory.fn = Trajectory.prototype;
	var __ = window.__ = Trajectory;
	
	//Extend method
	Trajectory.fn.initialize = function(){
		this.line = (this.options && this.options.line) || 'lineId';
		this.sku = (this.options &&this.options.sku) || 'sku';
		this.x = (this.options &&this.options.x) || 'x';
		this.y = (this.options &&this.options.y) || 'y';
		this.isSoldTime = (this.options &&this.options.isSoldTime) || 'isSoldTime';
		return this;
	};
	Trajectory.fn.play = function(){
		this.task = setInterval(this.drawTask,100 , 0);
	};
	Trajectory.fn.stop = function(){
		clearInterval(this.task);
	}
	Trajectory.fn.clear = function(){
		this.stop();
		this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
		this.index = 0;
		this.temp = [];
	}
	Trajectory.fn.toolbar = function(settings){
		for(var key in settings){
			$('#' + settings[key]).live('click' , this[key]);
		}
		return this;
	};
	Trajectory.fn.goto = function(index){		
		if(this.index > index){
			this.goback(index);
		}else if(this.index < index){
			this.goforward(index);
		}
	};
	// goback
	Trajectory.fn.goback = function(index){
		this.clear();
		for(var i = 0 ; i <= index ; i++){
			this.index = i;
			this.drawTrajectory();
		} 
	};
	Trajectory.fn.goforward = function(index){
		this.stop();
		for(var i = this.index ; i <= index ; i ++){
			this.index = i;
			this.drawTrajectory();
		}
	}
	Trajectory.fn.drawTask = function(){
		if(this.index >= this.list.length){
				clearInterval(this.task);
				return;
		}
		this.drawTrajectory();
		this.index++;
	};
	Trajectory.fn.drawTrajectory = function(){			
			var item = this.list[this.index];
			for(var i = 0 ; i < item.children.length ; i ++){
				this.drawPath(item.children[i]);
			}
	};
	Trajectory.fn.drawPath = function(path){
			var t = this.temp[path[this.line]];
			if(!t){
				this.temp[path[this.line]] = t = {"sku":path[this.sku] , "x":path[this.x] , "y":path[this.y] , "color":this.getRandomColor()};
			}
			if(t.x == path[this.x] && t.y == path[this.y]){
				return;
			}
			this.addLine(t.x , t.y ,path[this.x] , path[this.y] , t.color);
			if(path[this.sku] > 0){
				this.addCircle(path[this.x] , path[this.y], t.color);
			}
			if(path[this.isSoldTime] > 0){
				this.addSold(t.x , t.y , path[this.x] , path[this.y]);
			}
			this.temp[path[this.line]] = {"sku":path[this.sku] , "x":path[this.x] , "y":path[this.y] , "color":t.color};
		};
	Trajectory.fn.addLine = function(x1 , y1 , x2 , y2 , color){
			this.ctx.beginPath();
			this.ctx.strokeStyle = color;
			this.ctx.moveTo(x1 , y1);
			this.ctx.lineTo(x2 , y2);
			this.ctx.stroke();
			this.ctx.closePath();
		};
	Trajectory.fn.addSold = function(x1 , y1 , x2 , y2){
			var x = x2 - x1;
			var y = y2 - y1;
			var rate = Math.sqrt(Math.pow(x , 2) + Math.pow(y , 2));
			this.ctx.drawImage(this.money , x2 -  8 , y2 - 8);
		};
	Trajectory.fn.addCircle = function(x , y , color){
			this.ctx.beginPath();
			this.ctx.fillStyle = color;
			this.ctx.arc(x, y, this.circle_radius, 0, Math.PI*2, true);
			this.ctx.fill();
			this.ctx.closePath();
		};
	Trajectory.fn.getRandomColor = function() {
			var letters = '0123456789ABCDEF'.split('');
			var color = '#';
			for (var i = 0; i < 6; i++ ) {
				//color += letters[(i * this.index * 0x0E) % 15];
				color += letters[Math.round(Math.random(10) * 15)];
			}
			return color;
	};
	Trajectory.fn.setData = function(data){
		this.list = data;
	};	
}).call(this);